﻿Imports System.Runtime.InteropServices
Public Class GeneralForm
    <DllImport("uxtheme", CharSet:=CharSet.Unicode)> _
    Public Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal textSubAppName As String, ByVal textSubIdList As String) As Integer
    End Function
    Private WithEvents web As eWebbrowser.ExtendedWebBrowser
    Dim wassaved As Boolean
    Dim wherewassaved As String
    Public wascompiled As Boolean
    Public wherecompiled As String

#Region "ShowAll, Stylize and SaveAll"
    Public Sub showall()
        savemini.Enabled = True
        edit.Visible = True
        view.Visible = True
        gadgetsets.Visible = True
        undomini.Enabled = True
        redomini.Enabled = True
        cutmini.Enabled = True
        copymini.Enabled = True
        pastemini.Enabled = True
        explorermini.Enabled = True
        gadgetsetsmini.Enabled = True
        compilemini.Enabled = True
        savetool.Enabled = True
    End Sub



    Public Sub saveall()
        Try
            Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp"
            tabcontrol.SelectedIndex = 0
            For Each TabPage In tabcontrol.TabPages
                Dim writer As New System.IO.StreamWriter(path + "\" + tabcontrol.SelectedTab.Name, False, System.Text.Encoding.GetEncoding(1251))
                writer.Write(CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Text)
                writer.Close()
                tabcontrol.SelectedIndex = +1
            Next
        Catch ex As Exception
            notify.Text = "Failed to save the project(one of documents). Error:" + ex.Message
            notifier.Visible = True
        End Try
    End Sub

    Public Sub stylize()
        Try
            SetWindowTheme(ListView1.Handle, "explorer", Nothing)
        Catch ex As Exception

        End Try
        If My.Settings.style = "Aero Black" Then
            Dim style As New ToolStripProfessionalRenderer(New AeroBlack)
            MenuStrip1.Renderer = style
            ToolStrip1.Renderer = style
        ElseIf My.Settings.style = "Microsoft Office" Then
            Dim style As New ToolStripProfessionalRenderer(New MicrosoftOffice)
            MenuStrip1.Renderer = style
            ToolStrip1.Renderer = style
        ElseIf My.Settings.style = "Visual Studio" Then
            Dim style As New ToolStripProfessionalRenderer(New VisualStudio)
            MenuStrip1.Renderer = style
            ToolStrip1.Renderer = style
        End If
    End Sub
#End Region

    Private Sub GeneralForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        stylize()
        Dim startpage As New eWebbrowser.ExtendedWebBrowser
        general.Controls.Add(startpage)
        startpage.Dock = DockStyle.Fill
        startpage.WebBrowserShortcutsEnabled = False
        startpage.IsWebBrowserContextMenuEnabled = False
        startpage.ScriptErrorsSuppressed = True
        If My.Settings.webpage = True And My.Computer.Network.IsAvailable = True Then
            startpage.Navigate("http://po4teda.narod.ru/features/gadgetcreator/startpage/")
        Else
            startpage.Navigate(Application.StartupPath + "\help\index.htm")
        End If
        web = startpage
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\"
        Try
            If System.IO.Directory.Exists(path + "tmp\") = True Then
                System.IO.Directory.Delete(path + "tmp\", True)
            End If
        Catch ex As Exception

        End Try

        ' Проверка запуска
        Dim file As String = Command$()
        If file = "" Then

        Else
            file = Replace(file, Chr(34), "")
            Dim fileext = System.IO.Path.GetExtension(file)
            If fileext = ".gcp" Then
                Try
                    System.IO.Directory.CreateDirectory(path + "\tmp")
                    Using zip1 As Ionic.Zip.ZipFile = Ionic.Zip.ZipFile.Read(file)
                        Dim y As Ionic.Zip.ZipEntry
                        For Each y In zip1
                            y.Extract(path + "\tmp\", Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
                        Next
                    End Using
                    ' Создание редактора гаджета(начало)
                    tabcontrol.TabPages.Clear()
                    Dim gadgetpage As New TabPage
                    gadgetpage.Text = "Gadget"
                    gadgetpage.Name = "gadget.html"
                    Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                    htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                    If My.Settings.linenumbers = True Then
                        htmlbox.ShowLineNumbers = True
                    Else
                        htmlbox.ShowLineNumbers = False
                    End If
                    If System.IO.File.Exists(path + "\tmp\gadget.html") = True Then
                        Try
                            Dim reader As New System.IO.StreamReader(path + "\tmp\gadget.html", System.Text.Encoding.GetEncoding(1251))
                            htmlbox.Text = reader.ReadToEnd
                            reader.Close()
                        Catch ex As Exception
                            MsgBox("Error occured while gadget.html was opening. Gadget Creator will try to create default gadget.html file", 48, "Error!")
                            Dim writer As New System.IO.StreamWriter(path + "\tmp\gadget.html", False, System.Text.Encoding.GetEncoding(1251))
                            writer.Write(My.Settings.gadgetsyntax)
                            writer.Close()
                        End Try
                    Else
                        Dim writer As New System.IO.StreamWriter(path + "\tmp\gadget.html", False, System.Text.Encoding.GetEncoding(1251))
                        writer.Write(My.Settings.gadgetsyntax)
                        writer.Close()
                    End If
                    htmlbox.Dock = DockStyle.Fill
                    htmlbox.ContextMenuStrip = ContextMenuStrip1
                    gadgetpage.Controls.Add(htmlbox)
                    tabcontrol.TabPages.Add(gadgetpage)
                    tabcontrol.SelectedTab = gadgetpage

                    ' Создание редактора гаджета(конец)

                    browser.Checked = True
                    browsefiles.Visible = True
                    refreshfiles()
                    wassaved = True
                    wherewassaved = file
                    notifier.Visible = True
                    notify.Text = "Project loaded succesfully!"
                    showall()
                Catch ex As Exception
                    notifier.Visible = True
                    notify.Text = "Failed to load project. Error: " + ex.Message
                    Dim player As New System.Media.SoundPlayer
                    player.Stream = My.Resources.notfound
                    player.Play()
                End Try
            Else
                MsgBox("File with extension " + fileext + ", is not a project.", 48, "Fail")
                Dim player As New System.Media.SoundPlayer
                player.Stream = My.Resources.notfound
                player.Play()
            End If
        End If
    End Sub
    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click, newmini.Click
        If MsgBox("All unsaved data will be lost. Continue?", 36, "Gadget Creator") = MsgBoxResult.Yes Then
            Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
            Try
                If System.IO.Directory.Exists(path) = True Then
                    System.IO.Directory.Delete(path, True)
                    System.IO.Directory.CreateDirectory(path)
                Else
                    System.IO.Directory.CreateDirectory(path)
                End If
            Catch ex As Exception
                MsgBox("Error occured while temporary folder was deleting: " + ex.Message, 64, "Error")
                Exit Sub
            End Try
            tabcontrol.TabPages.Clear()
            Dim gadgetpage As New TabPage
            ' Удаление временной директории
            Try
                Dim writer As New System.IO.StreamWriter(path + "gadget.xml", True, System.Text.Encoding.UTF8)
                writer.Write(My.Resources.xmldefault)
                writer.Close()
                ' Создание редактора гаджета(начало)

                gadgetpage.Text = "Gadget"
                Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                If My.Settings.linenumbers = True Then
                    htmlbox.ShowLineNumbers = True
                Else
                    htmlbox.ShowLineNumbers = False
                End If
                htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                htmlbox.Text = My.Settings.gadgetsyntax
                htmlbox.Dock = DockStyle.Fill
                If My.Settings.linenumbers = True Then
                    htmlbox.ShowLineNumbers = True
                Else
                    htmlbox.ShowLineNumbers = False
                End If
                htmlbox.ContextMenuStrip = ContextMenuStrip1
                gadgetpage.Controls.Add(htmlbox)
                gadgetpage.Name = "gadget.html"
                tabcontrol.TabPages.Add(gadgetpage)
                Dim htmlwriter As New System.IO.StreamWriter(path + "gadget.html", True, System.Text.Encoding.GetEncoding(1251))
                htmlwriter.Write(htmlbox.Text)
                htmlwriter.Close()

                ' Создание редактора гаджета(конец)

                showall()

            Catch ex As Exception
                notify.Text = "While project was creating, an error occurred: " + ex.Message
            End Try

            browser.Checked = True
            explorermini.Checked = True
            browsefiles.Visible = True
            refreshfiles()

            tabcontrol.SelectedTab = gadgetpage
            wassaved = False
            If My.Settings.professional = True Then
                XMLEditor.Show()
            Else
                GadgetSetForm.Show()
            End If
        End If
        wassaved = False
        wherewassaved = ""
    End Sub
    Private Sub СвойстваГаджетаToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles СвойстваГаджетаToolStripMenuItem.Click, gadgetsetsmini.Click
        If My.Settings.professional = True Then
            XMLEditor.Show()
        Else
            GadgetSetForm.Show()
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click, openmini.Click
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        If MsgBox("All unsaved data will be lost! Continue?", 36, "Gadget Creator") = MsgBoxResult.Yes Then
            Try
                If System.IO.Directory.Exists(path) = True Then
                    System.IO.Directory.Delete(path, True)
                    System.IO.Directory.CreateDirectory(path)
                Else
                    System.IO.Directory.CreateDirectory(path)
                End If
            Catch ex As Exception
                MsgBox(ex.Message, 16, "Gadget Creator")
                Exit Sub
            End Try
            tabcontrol.TabPages.Clear()
            Dim ofd As New OpenFileDialog
            ofd.Filter = "Gadget Creator project|*.gcp"
            ofd.Title = "Open project"
            ofd.AddExtension = False
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim fileext As String = System.IO.Path.GetExtension(ofd.FileName)
                If fileext = ".gcp" Then
                    Try
                        Using zip1 As Ionic.Zip.ZipFile = Ionic.Zip.ZipFile.Read(ofd.FileName)
                            Dim y As Ionic.Zip.ZipEntry
                            For Each y In zip1
                                y.Extract(path, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
                            Next
                        End Using
                    Catch ex As Exception
                        notify.Text = "Project is broken and can not be loaded(project type is incorrect)!"
                        notifier.Visible = True
                        System.IO.Directory.Delete(path, True)
                        Exit Sub
                    End Try

                    Try
                        If System.IO.File.Exists(path + "\gadget.xml") = False Then
                            notify.Text = "Project is broken and can not be loaded(gadget.xml doesn't exists)!"
                            notifier.Visible = True
                            System.IO.Directory.Delete(path, True)
                            Exit Sub
                        Else
                            tabcontrol.TabPages.Clear()
                            If System.IO.File.Exists(path + "\gadget.html") = False Then
                                Dim creategadgetfile As New System.IO.StreamWriter(path)
                                creategadgetfile.Write(My.Settings.gadgetsyntax)
                                creategadgetfile.Close()
                                Dim gadgetpage As New TabPage
                                gadgetpage.Name = "gadget.html"
                                gadgetpage.Text = "Gadget"
                                Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                                htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                                If My.Settings.linenumbers = True Then
                                    htmlbox.ShowLineNumbers = True
                                Else
                                    htmlbox.ShowLineNumbers = False
                                End If
                                htmlbox.Text = My.Settings.gadgetsyntax
                                htmlbox.Dock = DockStyle.Fill
                                htmlbox.ContextMenuStrip = ContextMenuStrip1
                                gadgetpage.Controls.Add(htmlbox)
                                tabcontrol.TabPages.Add(gadgetpage)
                            Else
                                Dim opengadgetfile As New System.IO.StreamReader(path + "\gadget.html", System.Text.Encoding.GetEncoding(1251))
                                Dim gadgetpage As New TabPage
                                gadgetpage.Name = "gadget.html"
                                gadgetpage.Text = "Gadget"
                                Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                                htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                                If My.Settings.linenumbers = True Then
                                    htmlbox.ShowLineNumbers = True
                                Else
                                    htmlbox.ShowLineNumbers = False
                                End If
                                htmlbox.Text = opengadgetfile.ReadToEnd()
                                opengadgetfile.Close()
                                htmlbox.Dock = DockStyle.Fill
                                htmlbox.ContextMenuStrip = ContextMenuStrip1
                                gadgetpage.Controls.Add(htmlbox)
                                tabcontrol.TabPages.Add(gadgetpage)
                                tabcontrol.SelectedTab = gadgetpage
                                wassaved = True
                                wherewassaved = ofd.FileName
                                explorermini.Checked = True
                                browser.Checked = True
                                browsefiles.Visible = True
                                refreshfiles()
                            End If
                            showall()
                            notifier.Visible = True
                            notify.Text = "Project loaded succesfully!"

                        End If
                    Catch ex As Exception
                        notifier.Visible = True
                        notify.Text = "While project was opening, error occurred: " + ex.Message
                    End Try

                Else
                    notifier.Visible = True
                    notify.Text = "File with extension " + fileext + ", is not Gadget Creator project!"
                End If
            End If
        Else

        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savetool.Click, savemini.Click
        Dim selectedtab = tabcontrol.SelectedTab
        If wassaved = True Then
            Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
            saveall()
            Try
                Using zip As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile()
                    Dim filenames As String() = System.IO.Directory.GetFiles(path)
                    zip.AddFiles(filenames, "")
                    zip.Save(wherewassaved)
                End Using
                notify.Text = "Project was saved succesfully!"
                notifier.Visible = True
                tabcontrol.SelectedTab = selectedtab
            Catch ex As Exception
                notify.Text = "Failed to save the project! Error: " + ex.Message
                notifier.Visible = True
            End Try
        Else
            Dim sfd As New SaveFileDialog
            sfd.Filter = "Gadget Creator project|*.gcp"
            sfd.Title = "Save project"
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                saveall()
                Try
                    Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
                    Using zip As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile()
                        Dim filenames As String() = System.IO.Directory.GetFiles(path)
                        zip.AddFiles(filenames, "")
                        zip.Save(sfd.FileName)
                    End Using
                    wassaved = True
                    wherewassaved = sfd.FileName
                    notify.Text = "Project was saved succesfully!"
                    tabcontrol.SelectedTab = selectedtab
                    notifier.Visible = True
                Catch ex As Exception
                    notify.Text = "Failed to save the project! Error: " + ex.Message
                    notifier.Visible = True
                End Try
            End If
        End If
    End Sub

    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click, undomini.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Undo()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click, redomini.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Redo()
    End Sub

    Private Sub cut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cut.Click, cutmini.Click, cutcon.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Cut()
    End Sub

    Private Sub copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copy.Click, copymini.Click, copycon.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Copy()
    End Sub

    Private Sub paste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles paste.Click, pastemini.Click, pastecon.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Paste()
    End Sub

    Private Sub selectall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles selectall.Click, selectallcon.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).SelectAll()
    End Sub

    Private Sub КомпилироватьToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles КомпилироватьToolStripMenuItem.Click, compilemini.Click
        Try
            saveall()
            '  Начать компиляцию
            ToolStrip1.Enabled = False
            MenuStrip1.Enabled = False
            Dim sfd As New SaveFileDialog
            sfd.Filter = "Windows Sidebar gadget|*.gadget"
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
                Using zip As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile()
                    Dim filenames As String() = System.IO.Directory.GetFiles(path)
                    zip.AddFiles(filenames, "")
                    zip.Save(sfd.FileName)
                End Using
                notify.Text = "Windows Sidebar gadget was compiled succesfully!"
                notifier.Visible = True
                Try
                    Dim player As New System.Media.SoundPlayer
                    player.Stream = My.Resources.compilecomplete
                    player.Play()
                Catch ex As Exception

                End Try
            End If
            ToolStrip1.Enabled = True
            MenuStrip1.Enabled = True
        Catch ex As Exception
            notify.Text = "Error occurred when you tried to compile project. Error: " + ex.Message
            notifier.Visible = True
            ToolStrip1.Enabled = True
            MenuStrip1.Enabled = True
            Dim player As New System.Media.SoundPlayer
            player.Stream = My.Resources.errorcompile
            player.Play()
        End Try
    End Sub
    Public Sub refreshfiles()
        Try
            ListView1.Items.Clear()
            Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp"
            Dim folderInfo As New IO.DirectoryInfo(path)
            Dim arrFilesInFolder() As IO.FileInfo
            Dim fileInFolder As IO.FileInfo
            arrFilesInFolder = folderInfo.GetFiles("*.html")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 0
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.xml")
            For Each fileInFolder In arrFilesInFolder
                If fileInFolder.Name = "gadget.xml" Or fileInFolder.Name = "Gadget.xml" Then
                    ListView1.Items.Add("Gadget Settings").StateImageIndex = 1
                Else
                    ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 1
                End If
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.js")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 2
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.txt")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 3
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.css")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 4
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.bmp")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 5
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.gif")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 6
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.jpg")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 7
            Next
            arrFilesInFolder = folderInfo.GetFiles("*.png")
            For Each fileInFolder In arrFilesInFolder
                ListView1.Items.Add(fileInFolder.Name).StateImageIndex = 8
            Next
        Catch ex As Exception
            notifier.Visible = True
            notify.Text = ex.Message
        End Try

    End Sub

    Private Sub ListView1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        tabcontrol.SelectedIndex = 0
        deletebut.Enabled = True
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        If ListView1.SelectedItems.Count > 0 Then
            Dim type = ListView1.FocusedItem.Text
            If type = "gadget.xml" Or type = "Gadget.xml" Or type = "Gadget Settings" Then
                If My.Settings.professional = True Then
                    XMLEditor.Show()
                Else
                    GadgetSetForm.Show()
                End If
                deletebut.Enabled = False
            End If
            If type.EndsWith(".html") = True Then
                If tabcontrol.TabPages.ContainsKey(type) = True Then
                    tabcontrol.SelectTab(type)
                Else
                    Dim editor As New TabPage
                    editor.Text = ListView1.FocusedItem.Text
                    editor.Name = ListView1.FocusedItem.Text
                    Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                    htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                    If My.Settings.linenumbers = True Then
                        htmlbox.ShowLineNumbers = True
                    Else
                        htmlbox.ShowLineNumbers = False
                    End If
                    htmlbox.ContextMenuStrip = ContextMenuStrip1
                    Dim reader As New System.IO.StreamReader(path + ListView1.FocusedItem.Text, System.Text.Encoding.GetEncoding(1251))
                    htmlbox.Text = reader.ReadToEnd
                    reader.Close()
                    htmlbox.Dock = DockStyle.Fill
                    editor.Controls.Add(htmlbox)
                    tabcontrol.TabPages.Add(editor)
                    tabcontrol.SelectedTab = editor
                End If
            End If
            If type.EndsWith(".js") = True Then
                If tabcontrol.TabPages.ContainsKey(type) = True Then
                    tabcontrol.SelectTab(type)
                Else
                    Dim editor As New TabPage
                    editor.Text = ListView1.FocusedItem.Text
                    editor.Name = ListView1.FocusedItem.Text
                    Dim jsbox As New FastColoredTextBoxNS.FastColoredTextBox
                    If My.Settings.linenumbers = True Then
                        jsbox.ShowLineNumbers = True
                    Else
                        jsbox.ShowLineNumbers = False
                    End If
                    jsbox.Language = FastColoredTextBoxNS.Language.JS
                    Dim reader As New System.IO.StreamReader(path + ListView1.FocusedItem.Text, System.Text.Encoding.GetEncoding(1251))
                    jsbox.Text = reader.ReadToEnd
                    reader.Close()
                    jsbox.Dock = DockStyle.Fill
                    jsbox.ContextMenuStrip = ContextMenuStrip1
                    editor.Controls.Add(jsbox)
                    tabcontrol.TabPages.Add(editor)
                    tabcontrol.SelectedTab = editor
                End If
            End If
            If type.EndsWith(".css") = True Then
                If tabcontrol.TabPages.ContainsKey(type) = True Then
                    tabcontrol.SelectTab(type)
                Else
                    Dim editor As New TabPage
                    editor.Text = ListView1.FocusedItem.Text
                    editor.Name = ListView1.FocusedItem.Text
                    Dim cssbox As New FastColoredTextBoxNS.FastColoredTextBox
                    If My.Settings.linenumbers = True Then
                        cssbox.ShowLineNumbers = True
                    Else
                        cssbox.ShowLineNumbers = False
                    End If
                    Dim reader As New System.IO.StreamReader(path + ListView1.FocusedItem.Text, System.Text.Encoding.GetEncoding(1251))
                    cssbox.Text = reader.ReadToEnd
                    cssbox.ContextMenuStrip = ContextMenuStrip1
                    reader.Close()
                    cssbox.Dock = DockStyle.Fill
                    editor.Controls.Add(cssbox)
                    tabcontrol.TabPages.Add(editor)
                    tabcontrol.SelectedTab = editor
                End If
            End If
            If type.EndsWith(".txt") = True Then
                If tabcontrol.TabPages.ContainsKey(type) = True Then
                    tabcontrol.SelectTab(type)
                Else
                    Dim editor As New TabPage
                    editor.Text = ListView1.FocusedItem.Text
                    editor.Name = ListView1.FocusedItem.Text
                    Dim txtbox As New FastColoredTextBoxNS.FastColoredTextBox
                    If My.Settings.linenumbers = True Then
                        txtbox.ShowLineNumbers = True
                    Else
                        txtbox.ShowLineNumbers = False
                    End If
                    txtbox.Language = FastColoredTextBoxNS.Language.Custom
                    Dim reader As New System.IO.StreamReader(path + ListView1.FocusedItem.Text, System.Text.Encoding.GetEncoding(1251))
                    txtbox.Text = reader.ReadToEnd
                    txtbox.ContextMenuStrip = ContextMenuStrip1
                    reader.Close()
                    txtbox.Dock = DockStyle.Fill
                    editor.Controls.Add(txtbox)
                    tabcontrol.TabPages.Add(editor)
                    tabcontrol.SelectedTab = editor
                End If
            End If
            If type.EndsWith(".bmp") = True Or type.EndsWith(".gif") = True Or type.EndsWith(".jpg") = True Or type.EndsWith(".png") = True Then
                If type.EndsWith(".png") = True Then
                    ImageViewer.png()
                    ImageViewer.PictureZoom1.Load(path + ListView1.FocusedItem.Text)
                    ImageViewer.Show()
                Else
                    ImageViewer.PictureZoom1.Load(path + ListView1.FocusedItem.Text)
                    ImageViewer.Show()
                End If
                ImageViewer.Text = path + ListView1.FocusedItem.Text
            End If
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles create.Click, addnew.Click
        CreateFile.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deletebut.Click, delete.Click
        If ListView1.SelectedItems.Count > 0 Then
            If ListView1.FocusedItem.Text = "gadget.xml" Then
                Exit Sub
            Else

                If tabcontrol.TabPages.ContainsKey(ListView1.FocusedItem.Text) = True Then
                    If MsgBox("Are you sure? File will be deleted from the computer?", 36, "Delete file?") Then
                        tabcontrol.SelectTab(ListView1.FocusedItem.Text)
                        tabcontrol.TabPages.Remove(tabcontrol.SelectedTab)
                        Try
                            Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
                            IO.File.Delete(path + ListView1.FocusedItem.Text)
                            refreshfiles()
                        Catch ex As Exception
                            notify.Text = "File was not deleted... Error: " + ex.Message
                            notifier.Visible = True
                        End Try
                    End If
                Else
                End If
            End If
        Else
            notify.Text = "File was not deleted... Sorry!"
            notifier.Visible = True
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub ОбозревательФайловToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles browser.Click, explorermini.Click
        If browser.Checked = True Then
            browser.Checked = False
            explorermini.Checked = False
            browsefiles.Visible = False
            refreshfiles()
        Else
            browser.Checked = True
            browsefiles.Visible = True
            explorermini.Checked = True
            refreshfiles()
        End If
    End Sub
    Private Sub ОПрограммеToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ОПрограммеToolStripMenuItem.Click, helpmini.Click
        About.Show()
    End Sub

    Private Sub ВызовСправкиToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ВызовСправкиToolStripMenuItem.Click
        Process.Start("http://gadgetcreator.codeplex.com")
    End Sub

    Dim counting = 0
    Private Sub waiting_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles waiting.Tick
        If counting = 7 Then
            notifier.Visible = False
            counting = 0

        Else
            counting = counting + 1
        End If
    End Sub

    Private Sub notifier_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles notifier.VisibleChanged
        If notifier.Visible = True Then
            waiting.Start()
        Else
            waiting.Stop()
        End If
    End Sub

    Private Sub GeneralForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp"
        If System.IO.Directory.Exists(path) = True Then
            Try
                System.IO.Directory.Delete(path, True)
            Catch ex As Exception

            End Try
        Else

        End If
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        SettingsForm.Show()
    End Sub

    Private Sub ContextMenuStrip2_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip2.Opening
        delete.ToolTipText = ""
        rename.ToolTipText = ""
        If ListView1.SelectedItems.Count > 0 Then
            If ListView1.FocusedItem.Text = "gadget.xml" Or ListView1.FocusedItem.Text = "Gadget Settings" Then
                delete.Enabled = False
                rename.Enabled = False
                delete.ToolTipText = "You can't remove Gadget Settings file!"
                rename.ToolTipText = "You can't rename Gadget Settings file!"
            Else
                delete.Enabled = True
                rename.Enabled = True
                create.Visible = False
                addexisting.Visible = False
            End If
        Else
            delete.Enabled = False
            rename.Enabled = False
            create.Visible = True
            addexisting.Visible = True
        End If
    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        If CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).SelectedText = "" Then
            cutcon.Enabled = False
            copycon.Enabled = False
        Else
            cutcon.Enabled = True
            copycon.Enabled = True
        End If
        If Clipboard.ContainsText = False Then
            pastecon.Enabled = False
        Else
            pastecon.Enabled = True
        End If
    End Sub
    Public newfilename
    Private Sub rename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rename.Click
        RenameDialog.imagerename = False
        Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        Dim filename = ListView1.FocusedItem.Text
        Dim filetype = System.IO.Path.GetExtension(path + filename)
        If filetype = ".html" Or filetype = ".css" Or filetype = ".js" Or filetype = ".vbs" Or filetype = ".txt" Then
            ' If it's an editable file:
            If tabcontrol.TabPages.ContainsKey(filename) = True Then
                tabcontrol.SelectTab(filename)
                Try
                    Dim writer As New System.IO.StreamWriter(path + filename, False, System.Text.Encoding.GetEncoding(1251))
                    writer.Write(CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).Text)
                    writer.Close()
                    ' Now... Try to rename
                    RenameDialog.ext = filetype
                    RenameDialog.Show()
                    RenameDialog.lastname = path + filename
                    refreshfiles()
                Catch ex As Exception
                    MsgBox("Error occured. File was not saved! Error: " + ex.Message)
                    Exit Sub
                End Try
            End If
        ElseIf filetype = ".png" Or filetype = ".jpg" Or filetype = ".bmp" Or filetype = ".gif" Then
            ' Now... Try to rename
            RenameDialog.ext = filetype
            RenameDialog.lastname = path + ListView1.FocusedItem.Text
            RenameDialog.Show()
            refreshfiles()
        Else
            MsgBox("File with extension" + filetype + " is currently unsupported!", 48, "Unsupported!")
        End If

    End Sub

    Private Sub SplitButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitButton1.Click
        CreateFile.Show()
    End Sub

    Private Sub importfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles importfile.Click, addexisting.Click
        CreateFile.Show()
        CreateFile.Button2.PerformClick()
    End Sub

    Private Sub FindAndReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindAndReplaceToolStripMenuItem.Click, replacer.Click
        CType(tabcontrol.SelectedTab.Controls(0), FastColoredTextBoxNS.FastColoredTextBox).ShowReplaceDialog()
    End Sub

    Private Sub SendFeedbackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendFeedbackToolStripMenuItem.Click
        Feedback.Show()
    End Sub

    Private Sub web_NavigatingExtended(sender As Object, e As eWebbrowser.WebBrowserNavigatingExtendedEventArgs) Handles web.NavigatingExtended
        If e.Url.ToString.EndsWith("gcp") Then
            e.Cancel = True
            DownloadProject.Tag = e.Url.ToString
            DownloadProject.Show()
        Else
        End If
    End Sub
End Class